package com.service.infocenter.imp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import com.mapper.infocenter.RulePointMapper;
import com.mapper.infocenter.UserMapper;
import com.po.LoginHistoryInfo;
import com.po.MyDownloadInfo;
import com.po.PointLimitInfo;
import com.po.RulePointInfo;
import com.service.infocenter.RulePointService;
@Service
public class RulePointServiceImp implements RulePointService{
    @Resource(name="rulePointMapper")
    private RulePointMapper rulePointMapper;
    @Resource(name="userMapper")
    private UserMapper userMapper;
	@Override
	public List<RulePointInfo> getRulePointList(RulePointInfo rulePointInfo) {
		List<RulePointInfo> list=rulePointMapper.getRulePointList(rulePointInfo);
		return list;
	}

	@Override
	public int getRulePointCount(RulePointInfo rulePointInfo) {
		int i=rulePointMapper.getRulePointCount(rulePointInfo);
		return i;
	}

	public int updateUserpoint(@Param("user_id")String userid,@Param("point")int point){
		int i = rulePointMapper.updateUserpoint(userid,point);
		return i;
	}

	@Override
	public int getRulePointNumber(String description) {
		int i=rulePointMapper.getRulePointNumber(description);
		return i;
	}
    /*判断用户今日是否初次登录*/
	@Override
	public int haveLogin(String userid) {
		Date dt=new Date();
	    SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd");
	    System.out.println(matter1.format(dt));	
		LoginHistoryInfo loginHistoryInfo=new LoginHistoryInfo();
		loginHistoryInfo.setUser_id(userid);
	    loginHistoryInfo.setLogin_time(matter1.format(dt));
	    List<LoginHistoryInfo> list=rulePointMapper.getLoginHistory(loginHistoryInfo);
	    if(list.size()==1){
	    	return 0;
	    }
	    else return 1;
	}
    /*登陆后调用该方法更新积分*/
	@Override
	public int login(String userid) {
		int i=haveLogin(userid);
		if(i==0){
			/*用户今天初次登录*/
			int point=getRulePointNumber("每天初次登录获取积分数");
			updateUserpoint(userid,point); 
		}
		return 1;
	}

	@Override
	public int infoupload(String userid) {
		int point=getRulePointNumber("每上传一条知识获取积分数");
		updateUserpoint(userid,point); 
		return 1;
	}

	@Override
	public int infodownload(String userid, String infoid) {
		List<MyDownloadInfo> list =rulePointMapper.getMyDownloadList(userid, infoid);
		int point=0;
		if(list.size()==0){
			/*初次下载*/
			 point=getRulePointNumber("每个用户初次下载知识扣除积分");
			 updateUserpoint(userid,point);
		}
		return 1;
	}

	@Override
	public int illegalMessage(String userid) {
		int point=getRulePointNumber("每条非法评论扣除积分数");
		updateUserpoint(userid,point); 
		return 1;
	}

	@Override
	public int waterMessage(String userid) {
		int point=getRulePointNumber("评论被知识管理员判灌水无效评论扣除积分");
		updateUserpoint(userid,point); 
		return 1;
	}

	@Override
	public int addMessage(String userid,String content) {
		int count=getRulePointNumber("每条有效评论不少于字数");	
		if(content.length()>count){
			PointLimitInfo info=rulePointMapper.getPointLimit(userid);
			
			int limitpoint=getRulePointNumber("评论每天获取积分数上限为");
			int point=getRulePointNumber("每评论一条知识获取积分数");
			if(info!=null){
				if(info.getPoint()<limitpoint){					
					if((info.getPoint()+point)<=limitpoint){
						rulePointMapper.updatePointLimit(userid, info.getPoint()+point);
						updateUserpoint(userid,point); 
					}
					if((info.getPoint()+point)>limitpoint){
						rulePointMapper.updatePointLimit(userid, limitpoint);
						updateUserpoint(userid,limitpoint-info.getPoint()); 
					}
				}				
			}
			else {
				PointLimitInfo pointLimitInfo=new PointLimitInfo();
				pointLimitInfo.setUser_id(userid);
				pointLimitInfo.setPoint(point);		
				rulePointMapper.addPointLimit(pointLimitInfo);	
				updateUserpoint(userid,point);
			}
		}		
		return 1;
	}

	@Override
	public int getRegisterPoint(String roletype) {
		// TODO Auto-generated method stub
		int point = rulePointMapper.getRegisterPoint(roletype);
		return point;
	}

	public int getPersonalPoint(String userid){
		
		int point = rulePointMapper.getPersonalPoint(userid);
		return point;
	}
}
